<?php

namespace App\Models\Common;

use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Configure extends Model
{
	use HasDateTimeFormatter;
    use SoftDeletes;

    protected $fillable = ['key', 'value'];

    public static function config($key, $value = null)
    {
        if ($value == null) {
            $config = self::where('key', $key)->first();
            return $config ? $config->value : '';
        } else {
            self::updateOrCreate(
                ['key'=>$key],
                ['value'=>$value]
            );
            return $value;
        }
    }

    public static function cache($key, $default = '')
    {
        $config = self::where('key', $key)->first();
        return $config ? $config->value : $default;
    }

    public static function int($key, $value = null)
    {
        if ($value == null) {
            $value = self::config($key);
            return @intval($value);
        } else {
            self::updateOrCreate(
                ['key'=>$key],
                ['value'=>($value.'')]
            );
            return $value;
        }
    }
}
